package com.study.io.bio.demo;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerNormal {

	private static int DEFAULT_PORT = 12345;
	private static ServerSocket server;
	public static void start() throws IOException{
		start(DEFAULT_PORT);
	}
	
	public synchronized static void start(int port) throws IOException {

		if(server != null) return ;
		try{
			
			server = new ServerSocket(port);
			System.out.println("服务器启动, 端口号: " + port);
			
			while(true){
				Socket socket = server.accept();
				new Thread(new ServerHandler(socket)).start();
			}
		}finally {
			if(server != null){
				System.out.println("服务器已关闭");
			}
		}
	}
}
